package com.qk.ezbuy.controller;

import com.github.pagehelper.PageInfo;
import com.qk.ezbuy.pojo.Product;
import com.qk.ezbuy.pojo.ProductCar;
import com.qk.ezbuy.pojo.User;
import com.qk.ezbuy.service.ProductService;
import com.qk.ezbuy.service.UserService;
import com.qk.ezbuy.util.Myutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    private ProductService productService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/getcar")
    public Map getcar(String uuid) {
        User user = null;
        Map<String, Object> map = new HashMap<String, Object>();
        if(uuid !="" && uuid !=null){
            if (redisTemplate.hasKey(uuid)) {
                user = (User) redisTemplate.opsForValue().get(uuid);
                //用户购物车和收藏
                List<ProductCar> listcar = new ArrayList<ProductCar>();
                if (redisTemplate.hasKey(user.getLoginname() + "car")) {
                    listcar = (List) redisTemplate.opsForValue().get(user.getLoginname() + "car");
                } else {
                    redisTemplate.opsForValue().set(user.getLoginname() + "car", listcar);
                }
                List<ProductCar> listsc = new ArrayList<ProductCar>();
                if (redisTemplate.hasKey(user.getLoginname() + "sc")) {
                    listsc = (List) redisTemplate.opsForValue().get(user.getLoginname() + "sc");
                } else {
                    redisTemplate.opsForValue().set(user.getLoginname() + "sc", listsc);
                }
                Collections.reverse(listsc);
                map.put("user", user);
                map.put("listcar", listcar);
                map.put("listsc", listsc);
                map.put("msg", true);
            }
            map.put("user", user);
        }

        return map;
    }

    @RequestMapping("/addcar")
    public boolean addcar(String id, String loginname, String count,String buycarto) {
        List<Product> products = (List) redisTemplate.opsForValue().get(loginname + "car");
        if(products.size()<10){
            Product productx = productService.selectById(Integer.parseInt(id));
            if (count != null) {
                productx.setCount(Integer.parseInt(count));
            }
            if(buycarto !=null){
                for (Product product : products) {
                    if (product.getId().equals(productx.getId())) {
                        product.setCount(productx.getCount());
                        redisTemplate.opsForValue().set(loginname + "car", products);
                        return true;
                    }
                }
            }else {
                for (Product product : products) {
                    if (product.getId().equals(productx.getId())) {
                        product.setCount(product.getCount() + productx.getCount());
                        redisTemplate.opsForValue().set(loginname + "car", products);
                        return true;
                    }
                }
                products.add(productx);
                redisTemplate.opsForValue().set(loginname + "car", products);
                return true;
            }
        }

        return false;
        }

        //购物车删除
        @RequestMapping("/delcar")
        public boolean delcar(String id,String loginname){
            List<Product> products = (List) redisTemplate.opsForValue().get(loginname + "car");
            if(id!="" && id !=null){
                for (int i=0;i<products.size();i++){
                    if (products.get(i).getId().equals(id)){
                        products.remove(i);
                        redisTemplate.opsForValue().set(loginname+"car",products);
                        return true;
                    }
                }
            }else {
                List<Product> productsx = new ArrayList<Product>();
                redisTemplate.opsForValue().set(loginname+"car",productsx);
                return true;
            }
            return false;
        }

        //加入收藏夹
        @RequestMapping("/addshoucang")
        public String addshoucang(String id,String loginname){
            if(loginname!=null && loginname !=""){
                List<Product> list = (List)redisTemplate.opsForValue().get(loginname+"sc");
                Product product = productService.selectById(Integer.parseInt(id));
                for(Product product1 : list){
                    if(product1.getId().equals(product.getId())){
                        return "已收藏。";
                    }
                }
                if(list.size()==6){
                    list.remove(0);
                    list.add(product);
                    redisTemplate.opsForValue().set(loginname+"sc",list);
                    return "收藏成功！";
                }
                list.add(product);
                redisTemplate.opsForValue().set(loginname+"sc",list);
                return "收藏成功！";
            }
            return "请先登陆！";
        }

        //清空收藏夹
    @RequestMapping("/delshoucang")
    public boolean delshoucang(String loginname){
            if(loginname !=null && loginname !=""){
                List<Product> list = new ArrayList<Product>();
                redisTemplate.opsForValue().set(loginname+"sc",list);
                List<Product> list1=(List)redisTemplate.opsForValue().get(loginname+"sc");
                return true;
            }
            return false;
    }
    @Autowired
    private UserService userService;

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/UserPage")
    public PageInfo<User> selectByPage(@RequestParam int pageNum, @RequestParam int pageSize){
        PageInfo<User> pageInfo=null;
        pageInfo=userService.selectPage(pageNum,pageSize);
        return pageInfo;
    }
    /**
     * 逻辑删除
     */
    @RequestMapping("UserDel")
    public Map UserDel(User user){
        Map<String,Object> map = new HashMap<>();
        user.setType(2);
        int count = userService.delUser(user);
        if(count>0){
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }
    /**
     * 回显
     */
    @RequestMapping("selectUser")
    public Map selectUser(int id){
        Map<String,Object> map = new HashMap<>();
        User user = userService.selectByID(id);
        map.put("user",user);
        return map;
    }
    /**
     * 重置密码以及修改个人信息
     */
    @RequestMapping("UserUpdate")
    public Map UserUpdate(User user){
        Map<String,Object> map = new HashMap<>();
        user.setPassword(Myutil.md5(user.getPassword()));
        int count = userService.updateUser(user);
        if(count>0){
            System.out.println(count);
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }
    //修改信息
    @RequestMapping("UserUpdateById")
    public Map UserUpdateById(User user){
        System.out.println("==============");
        System.out.println(user.getLoginname()+user.getMobile()+user.getEmail());
        Map<String,Object> map = new HashMap<>();
        user.setPassword(Myutil.md5(user.getPassword()));
        int count = userService.updateUser(user);
        System.out.println(count);
        if(count>0){
            System.out.println(count);
            map.put("msg",1);
        }else{
            map.put("msg",2);
        }
        return map;
    }
    }